package com.example.owner.lettery1.utils

import android.content.Context
import android.content.Intent
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.support.v4.content.FileProvider
import com.example.owner.lettery1.BuildConfig

import java.io.File

object InstallUtil {

    /**
     * 安装包
     */
     fun installApk(filePath: String,context: Context) {
        val intent = Intent(Intent.ACTION_VIEW)
        val file = File(filePath)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // 7.0+以上版本
            val apkUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileProvider", file)
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            intent.setDataAndType(apkUri, "application/vnd.android.package-archive")
        } else {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive")
        }
        context.startActivity(intent)
    }

    /**
     * 检查包是否存在
     ** @param packageName
     */
     fun checkPackageInfo(packageName: String): Boolean {
        var packageInfo: PackageInfo? = null
        try {
            packageInfo = CoreApplication.instance!!.packageManager.getPackageInfo(packageName, 0)
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        return packageInfo != null
    }

}